package common.ComboboxCheckboxTree
{
	import flash.events.MouseEvent;
	
	import mx.controls.CheckBox;
	import mx.controls.treeClasses.TreeItemRenderer;
	import mx.controls.treeClasses.TreeListData;
	
	/**
	 * 
	 * @author zhangzhicheng
	 * 
	 */	
	public class CheckBoxTreeRenderer extends TreeItemRenderer
	{
		private var _checkBox:CheckBox;
		
		public function CheckBoxTreeRenderer()
		{
			super();
		}
		
		/**
		 *  @private
		 */
		override protected function createChildren():void
		{
			super.createChildren();
			
			if (!_checkBox) 
			{
				_checkBox = new CheckBox;
			}
			
			_checkBox.addEventListener(MouseEvent.MOUSE_DOWN, checkBoxMouseDownHandler);
			_checkBox.addEventListener(MouseEvent.CLICK, checkBoxClickHandler);
			
			addChild(_checkBox);
		}
		
		/**
		 *  @private
		 */
		override protected function measure():void
		{
			super.measure();
			
			if (isNaN(explicitWidth))
			{
				 
				measuredWidth += _checkBox.width + 4;
				measuredHeight = label.getExplicitOrMeasuredHeight();
			}
			else
			{
				if (_checkBox.measuredHeight > measuredHeight) 
					measuredHeight = _checkBox.measuredHeight;
			}
		}
		
		/**
		 *  @private
		 */		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			var treeData:TreeListData = (listData as TreeListData);
			var startx:Number =  treeData ? treeData.indent : 0;
			
			if (disclosureIcon)
			{
				disclosureIcon.x = startx;
				
				startx = disclosureIcon.x + disclosureIcon.width;
				
				disclosureIcon.setActualSize(disclosureIcon.measuredWidth,
					disclosureIcon.measuredHeight);
				
				disclosureIcon.visible = data ?
					treeData.hasChildren :
					false;
			}
			
			if (icon)
			{
				icon.x = startx;
				startx = icon.x + icon.measuredWidth;
				icon.setActualSize(icon.measuredWidth, icon.measuredHeight);
			}
			
			if (_checkBox)
			{
				_checkBox.x = startx + 2;
				
				startx = _checkBox.x + _checkBox.measuredWidth + 2;
				
				_checkBox.setActualSize(_checkBox.measuredWidth, _checkBox.measuredHeight);
				
				var isSelected:Boolean = (owner as TreeComboBoxRenderer).treeSelectedItems.indexOf(data) != -1;
				if (isSelected && enableSelection() == false)
				{
					(owner as TreeComboBoxRenderer).updateSelectedItems(data, false);
				}
				else
				{
					_checkBox.selected = isSelected;
				}
			}
			
			label.x = startx;
			label.setActualSize(unscaledWidth - startx, measuredHeight);
			
			var verticalAlign:String = getStyle("verticalAlign");
			if (verticalAlign == "top")
			{
				_checkBox.y = 0;
			}
			else if (verticalAlign == "bottom")
			{
				_checkBox.y = unscaledHeight - _checkBox.height;
			}
			else
			{
				_checkBox.y = (unscaledHeight - _checkBox.height) / 2;
			}
		}
		
		/**
		 * @private
		 */		
		protected function checkBoxClickHandler(event:MouseEvent):void
		{
			event.stopPropagation();
			if (enableSelection())
			{
				(owner as TreeComboBoxRenderer).updateSelectedItems(data, _checkBox.selected);
			}
			else
			{
				_checkBox.selected = false;
			}
		}
		
		/**
		 * @private
		 */		
		protected function checkBoxMouseDownHandler(event:MouseEvent):void
		{
			event.stopPropagation();	
		}
		
		/**
		 * 是否可以选择
		 */		
		protected function enableSelection():Boolean
		{
			var selectParentEnabled:Boolean = (owner as TreeComboBoxRenderer).outerDocument.isParentSelectable;
			var treeData:TreeListData = (listData as TreeListData);
			return (selectParentEnabled || treeData.hasChildren == false);
		}
	}
}